<template>
    <div class="tag">
         <ul>
            <li class="tags-li" v-for="(item,index) in tagsList" :class="{'active':item.path===$route.path||item.path==$route.fullPath}" :key="index">
                <router-link :to="item.path" class="tags-li-title">
                    {{item.title}}
                </router-link>
                <span class="tags-li-icon" @click="closeTag(index)"><i class="el-icon-close"></i></span>
            </li>
         </ul>
         <div class="tags-close-box">
            <el-dropdown  @command="handleCommand">
                <el-button size="mini" type="primary">
                    标签选项<i class="el-icon-arrow-down el-icon--right"></i>
                </el-button>
                <el-dropdown-menu size="small" slot="dropdown">
                    <el-dropdown-item command="other">关闭其他</el-dropdown-item>
                    <el-dropdown-item command="all">关闭所有</el-dropdown-item>
                </el-dropdown-menu>
            </el-dropdown>
        </div>
    </div>
</template>

<script>
    export default {
        name:'tag',
        data(){
            return{
                tagsList:[]
            }
        },
        methods:{
            setTags(route){
                const isExist=this.tagsList.some(item=>{
                    console.log(item)
                    return item.path===route.fullPath;
                })
               if(!isExist){
                   this.tagsList.push({
                       path: route.fullPath,
                       title:route.meta.title,
                       params:route.params
                   })
               }
            },
            closeTag(index){
               this.tagsList.splice(index,1);
               const item=this.tagsList[index]? this.tagsList[index]:this.tagsList[index-1];
               if(item){
                   this.$router.push(item.path);
               }else{
                   var IndexPath;
                   let username=JSON.parse(sessionStorage.getItem("loginInfo")).username||0;
                    if(username=="huangdairong"){
                        IndexPath="/profit"
                    }else if(username=="maqun"){
                        IndexPath="/accountRank"
                    }
                   this.$router.push(IndexPath);
               }
            },
            closeAll(){
                this.tagsList=[];
                var IndexPath;
                let username=JSON.parse(sessionStorage.getItem("loginInfo")).username||0;
                if(username=="huangdairong"){
                    IndexPath="/profit"
                }else if(username=="maqun"){
                    IndexPath="/accountRank"
                }
                this.$router.push(IndexPath);
            },
            closeOther(){
                let nowRoute=this.$route;
                this.tagsList=[];
                this.setTags(nowRoute);
            },
            handleCommand(command){
                command=='other'?this.closeOther():this.closeAll()
            }
        },
        created(){
            this.setTags(this.$route)
        },
        watch:{
            $route(newVal,oldVal){
              this.setTags(newVal);
            }
        }
    }
</script>

<style scoped lang="scss">
    .tag{
        position:relative;
        margin-bottom:20px;
        height: 30px;
        line-height: 30px;
        overflow: hidden;
        background: #fff;
    }
    .tag ul {
        box-sizing: border-box;
        width: 100%;
        height: 100%;
    }
    .tags-li {
        float: left;
        margin: 3px 5px 2px 3px;
        border-radius: 3px;
        font-size: 12px;
        overflow: hidden;
        cursor: pointer;
        height: 23px;
        line-height: 23px;
        border: 1px solid #e9eaec;
        background: #fff;
        padding: 0 5px 0 12px;
        vertical-align: middle;
        color: #666;
        -webkit-transition: all .3s ease-in;
        -moz-transition: all .3s ease-in;
        transition: all .3s ease-in;
    }
     .tags-close-box {
        position: absolute;
        right: 0;
        top: 0;
        box-sizing: border-box;
        padding-top: 1px;
        text-align: center;
        width: 110px;
        height: 30px;
        background: #fff;
        box-shadow: -3px 0 15px 3px rgba(0, 0, 0, .1);
        z-index: 10;
    }
    .tags-li.active .tags-li-title {
        color: #fff;
    }
    .tags-li:not(.active):hover {
        background: #f8f8f8;
    }

    .tags-li.active {
        color: #fff;
    }
    .tags-li.active {
        border: 1px solid #409EFF;
        background-color: #409EFF;
    }
</style>